package com.hjl.service.impl;

import com.hjl.dao.UserDao;
import com.hjl.dao.impl.UserDaoImpl;
import com.hjl.domain.PageBean;
import com.hjl.domain.User_inf;
import com.hjl.service.UserService;
import java.util.List;
import java.util.Map;

/**
 * 管理账户业务逻辑层实现类
 *
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/8 10:10
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    /**
     * 登录
     *
     * @param user 用户对象
     * @return com.hjl.domain.User_inf
     * @author Lenovo
     * @date 2021/10/8 10:10
     */
    @Override
    public User_inf login(User_inf user) {
        return userDao.findByNameAndPassword(user.getLoginname(), user.getPassword());
    }

    /**
     * 查找User（弃用）
     *
     * @param condition 状态
     * @return java.util.List<com.hjl.domain.User_inf>
     * @author Lenovo
     * @date 2021/10/8 10:10
     */
    @Override
    public List<User_inf> checkUser(Map<String, String[]> condition) {
        return userDao.findByCondition(condition);
    }

    /**
     * 分页查询
     *
     * @param currentPageString 当前页
     * @param rowsString        行数
     * @param condition         状态
     * @return com.hjl.domain.PageBean<com.hjl.domain.User_inf>
     * @author Lenovo
     * @date 2021/10/8 10:10
     */
    @Override
    public PageBean<User_inf> findUserByPage(String currentPageString, String rowsString, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(currentPageString);
        int rows = Integer.parseInt(rowsString);
        int totalCount = userDao.findTotalCount(condition);
        int start = (currentPage - 1) * rows;
        List<User_inf> list = userDao.findByPage(start, rows, condition);
        int totalPage = totalCount % rows == 0 ? totalCount / rows : (totalCount / rows + 1);

        //创建PageBean并放入数据，返回
        return new PageBean<>(totalCount, totalPage, list, currentPage, rows);
    }

    /**
     * 删除选中
     *
     * @param ids 选中的id
     * @author Lenovo
     * @date 2021/10/8 10:11
     */
    @Override
    public void delSelect(String[] ids) {
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                userDao.delUser(Integer.parseInt(id));
            }
        }
    }

    /**
     * 更新
     *
     * @param user 用户对象
     * @author Lenovo
     * @date 2021/10/8 10:12
     */
    @Override
    public void updateUser(User_inf user) {
        userDao.updateUser(user);
    }

    /**
     * 通过id查询
     *
     * @param id ID
     * @return com.hjl.domain.User_inf
     * @author Lenovo
     * @date 2021/10/8 10:12
     */
    @Override
    public User_inf findUserById(String id) {
        return userDao.findUserById(Integer.parseInt(id));
    }

    /**
     * 添加
     *
     * @param user 用户对象
     * @author Lenovo
     * @date 2021/10/8 10:12
     */
    @Override
    public void addUser(User_inf user) {
        userDao.addUser(user);
    }
}
